# 4、 实现一个 用户系统
# 实现 用户的 注册、登录、修改密码、删除用户
# 要求：用户名不能重复、数据必须存储在 json文件中
# 用户名 长度 必须在6-20位之间，删除用户，必须校验用户密码
# 修改密码，需要输入原密码与新密码，且 新密码不能与旧密码一致
# '''
# 欢迎来到xxx 用户系统，请输入您的操作指令
# 0 >>> 退出
# 1 >>> 注册
# 2 >>> 登录
# 3 >>> 修改密码
# 4 >>> 删除用户
import random
import time
import pymysql



def mydb(sql):
    db = pymysql.connect(user='test', password='test123', host='192.168.0.125', port=3306,
                         database='userinfo', charset='utf8mb4')
    cs = db.cursor()
    cs.execute(sql)
    data = cs.fetchall()
    db.commit()
    db.close()
    return data


def make_userid():
    uid = int(time.time())
    before = random.randint(11, 99)
    after = random.randint(10, 99)
    uid = f"{before}{uid}{after}"
    return uid


def check_pwd(pwd):
    """

    :param pwd: 传入密码是否符合要求
    :return:
    """
    if len(pwd) <= 8:
        return False
    else:
        a = b = c = d = 0
        for i in pwd:
            if i.isdigit():
                a = 1
            elif i.isupper():
                b = 1
            elif i.islower():
                c = 1
            else:
                d = 1
        if a + b + c + d < 3:
            return False
        else:
            return True



print('注册:1')
print('登录:2')
print('修改密码:3')
print('删除账号:4')
print('退出:0')

while True:
    flag = input('请输入您要做的指令：')
    if flag == '1':
        username = input('请输入用户名')
        if 6 <= len(username) <= 20:
            data = mydb(f"select * from user where uname = '{username}';")
            if len(data) == 0:
                password = input('请输入密码：')
                if check_pwd(pwd=password):
                    uid = make_userid()
                    mydb(f"insert into user values({uid},'{username}','{password}')")
                    print('注册成功')
                else:
                    print('密码不合法')
            else:
                print('用户已存在')
        else:
            print('用户名不合法')



    elif flag == '2':
        username = input('请输入用户名：')
        data = mydb(sql=f"select * from user where uname = '{username}';")
        if len(data)==0:
            print('用户名未注册')
        else:
            password = input('请输入密码')
            db_password = mydb(f"select upassword from user where uname = '{username}';")
            if password == db_password[0][0]:
                print('登陆成功')
            else:
                print('密码错误')

    elif flag == '3':
        username = input('请输入用户名')
        data = mydb(sql=f"select * from user where uname = '{username}';")
        if data == 0:
            print('用户名输入错误')
        else:
            password = input('请输入原密码')
            db_password = mydb(f"select upassword from user where uname = '{username}';")
            if password == db_password[0][0]:
                newpassword = input('请输入需要新的密码')
                if newpassword ==db_password[0][0]:
                    print('新密码不能和旧密码一致')
                elif check_pwd(pwd=password):
                     mydb(f"update user set upassword = '{newpassword}' where uname ='{username}';")
                     print('修改成功')


    elif flag == '4':
        username = input('请输入账号')
        data = mydb(sql=f"select * from user where uname = '{username}';")
        if len(data) ==0:
            print('用户名输入错误')
        else:
            password = input('请输入密码')
            db_password = mydb(f"select upassword from user where uname = '{username}';")
            if password ==db_password[0][0]:
                deluser = input('是否确认删除账号？输入y删除,其他任意键退出')
                if deluser =='y':
                    mydb(f"delete from user where uname = '{username}';")
                    print('账号已删除')
                else:
                    continue


    elif flag == '0':
        break  # 退出
    else:
        print('指令输入有误')

